package com.example.dell.fastshopping.user.modle;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.task.TaskType;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.http.Query;

public class TaskActivityViewMolde extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<BaseResponse> getIntegral = new MutableLiveData<>();
    private MutableLiveData<TaskType> taskList = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> share = new MutableLiveData<>();

    public MutableLiveData<BaseResponse> getTaskIntegral(int type, int id) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("retains", id);
        String json = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        RetrofitClient.execute(service.getIntegral(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                getIntegral.setValue(o);
            }
        });
        return getIntegral;
    }

    public MutableLiveData<TaskType> getTaskList() {
        RetrofitClient.execute(service.getTaskType(), new NetCallback<BaseResponse<TaskType>>() {
            @Override
            public void netCallback(BaseResponse<TaskType> response) {
                taskList.setValue(response.getData());
            }
        });
        return taskList;
    }

    public MutableLiveData<BaseResponse> share(int taskId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", taskId);
        map.put("weixinShare", 1);
        String s = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);
        RetrofitClient.execute(service.share(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                share.setValue(o);
            }
        });
        return share;
    }
}
